let requestUrl;

function addHandler() {
    requestUrl = "/jiejing_category/insert";

    $("#jiejing_dialog").dialog("open");
}

function editHandler() {
    let rowData = $("#jiejing_list").datagrid("getSelected");

    if (rowData) {
        requestUrl = "/jiejing_category/updateById";

        $("#id").val(rowData.id);
        $("#name").textbox("setValue", rowData.name);
        $("#note").textbox("setValue", rowData.note);
        $("#texiaoId").combobox("setValue", rowData.texiaoId);

        $("#jiejing_dialog").dialog("open");
    } else {
        alertMsg("请选择要修改的记录！", "warning");
    }
}

function viewHandler() {
    let rowData = $("#jiejing_list").datagrid("getSelected");

    if (rowData) {
        $("#jiejing_image").attr("src", rowData.image);
        $("#jiejing_name").html(rowData.name);
        $("#jiejing_note").html(rowData.note);

        ajaxGet("/equipment_texiao/selectById", {
            id: rowData.texiaoId
        }, function (resp) {
            let data = resp.data;

            $("#jiejing_function").html(data.name);
        }, error);

        $("#view_dialog").dialog("open");
    } else {
        alertMsg("请选择一条记录！");
    }
}

function imageHandler() {
    let rowData = $("#jiejing_list").datagrid("getSelected");

    if (rowData) {
        $("#jiejing_id").val(rowData.id);
        $("#img").attr("src", rowData.icon);

        $("#upload_dialog").dialog("open");
    } else {
        alertMsg("请选择一条记录！", "warning");
    }
}

$(document).ready(function() {

    $("#name").textbox({
        width: 150,
        required: true
    });

    $("#texiaoId").combobox({
        url: "/equipment_texiao/selectAll",
        valueField: "id",
        textField: "name",
        width: 150,
        editable: true,
        required: true,
        panelHeight: "auto"
    });

    $("#note").textbox({
        width: 372,
        height: 100,
        required: true,
        multiline: true
    });

    $("#jiejing_dialog").dialog({
        title: "结晶类型",
        buttons: [{
            iconCls: "icon-save",
            text: "保存",
            handler: function() {
                let selector = "#jiejing_form";

                checkForm(selector, function () {
                    let data = $(selector).serialize();

                    ajaxPost(requestUrl, data, function(resp) {
                        showMsg(resp.message);

                        $(selector).form("clear");
                        $("#jiejing_dialog").dialog("close");
                        $("#jiejing_list").datagrid("reload");
                    }, error);
                });
            }
        }, {
            iconCls: "icon-cancel",
            text: "取消",
            handler: function() {
                $("#jiejing_form").form("clear");
                $("#jiejing_dialog").dialog("close");
            }
        }]
    });

    /******************************************************************************************************/

    $("#view_dialog").dialog({
        title: " ",
        width: 240
    });

    /******************************************************************************************************/

    fetchUploadFilebox("#jiejing_list", "/jiejing_category/upload");

    fetchUploadDialog("/jiejing_category/updateImageById", function () {
        $("#jiejing_list").datagrid("reload");
    });

    /******************************************************************************************************/

    /**
     * 表格体的高度
     * @type {number}
     */
    let heigt_body = heigt_tr_with_image * 10;
    /**
     * 表格高度
     * @type {number}
     */
    let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

    $("#jiejing_list").datagrid({
        url: "/jiejing_category/selectByPage",
        method: "get",
        height: height_table,
        fitColumns: true,
        rownumbers: true,
        pagination: true,
        toolbar: [{
            iconCls: "icon-add",
            text: "添加",
            handler: function() {
                addHandler();
            }
        }, "-", {
            iconCls: "icon-edit",
            text: "修改",
            handler: function() {
                editHandler();
            }
        }, "-", {
            iconCls: "icon-search",
            text: "查看",
            handler: function() {
                viewHandler();
            }
        }, "-", imageUpdateButton],
        columns: [[
            {field: "name", title: "结晶名称", align: "center", width: 50},
            {field: "image", title: "图片", align: "center", width: 50
                , formatter: function(value) {
                    return "<img height='" + size + "' src='" + value + "' />";
                }
            },
            {field: "note", title: "说明", align: "center", width: 100
                , formatter: function(value) {
                    return "<div class='ell' title='" + value + "'>" + value + "</div>";
                }
            },
            {field: "texiaoId", title: "功能", align: "center", width: 50
                , formatter: function(value, rowData, rowIndex) {
                    if (value) {
                        ajaxGet("/equipment_texiao/selectById", {
                            id: value
                        }, function (resp) {
                            let data = resp.data;

                            $("#texiao_" + rowIndex).html(data.name);
                        }, error);

                        return "<div class='blue' id='texiao_" + rowIndex + "'></div>";
                    }

                    return "/";
                }
            }
        ]]
    });

});